package com.avyss.flyingpoints.scene;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Paint;

public class AbstractFlyingObject {
	
	protected final DynamicsParams params;
	
	protected float posX;		// X position (pixels)
	protected float posY;		// Y position (pixels)
	protected float velX;		// X-component of the velocity (pixels/msec)
	protected float velY;		// y-component of the velocity (pixels/msec)
	
	protected Random rand = new Random();
	
	public AbstractFlyingObject(DynamicsParams params) {
		this.params = params;
		
	    // Randomly place the flying point
    	posX = rand.nextInt(params.getWindowWidth());
    	posY = rand.nextInt(params.getWindowHeight());
    	velX = 0;
    	velY = 0;
	}
	
	public void applyMove(long timeStepMs) {
		posX += velX * timeStepMs;
		posY += velY * timeStepMs;
	}
	
	public void draw(Canvas canvas, Paint paint) {
		
	    float velMagnitude = (float) Math.sqrt(velX * velX + velY * velY);
	    if (velMagnitude < Float.MIN_NORMAL) {
	        velMagnitude = Float.MIN_NORMAL;
	    }
	   
	    float dx = velX / velMagnitude * params.getPointTraceLen();
	    float dy = velY / velMagnitude * params.getPointTraceLen();

	   	canvas.drawLine(posX - dx/2, posY - dy/2, posX + dx/2, posY + dy/2, paint);
	}
	
}
